package br.inf.solutions.apresentacoes.ssp.view.composer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;

import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.bulletin.BulletinAwaitItensController;
import br.inf.solutions.apresentacoes.ssp.control.bulletin.BulletinController;
import br.inf.solutions.apresentacoes.ssp.control.bulletin.BulletinPublishedItensController;
import br.inf.solutions.apresentacoes.ssp.control.bulletin.BulletinTopicsController;
import br.inf.solutions.apresentacoes.ssp.control.bulletin.BulletinTypeController;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.Bulletin;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.BulletinPublishedItem;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.BulletinTopics;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.BulletinType;

@SuppressWarnings("serial")
@org.springframework.stereotype.Component
@Scope("prototype")
public class RetificationBulletinComposer extends BaseComposer<Bulletin, BulletinController>  {
	
	protected final Integer BULLETINPARTS = 4;

	private Integer bulletinPart;
	
	private BulletinTopics bulletinTopic;
	
	private List<BulletinPublishedItem> itens;
	
	public Integer getBulletinPart() {
		return bulletinPart;
	}

	public void setBulletinPart(Integer bulletinPart) {
		this.bulletinPart = bulletinPart;
	}

	public BulletinTopics getBulletinTopic() {
		return bulletinTopic;
	}

	public void setBulletinTopic(BulletinTopics bulletinTopic) {
		this.bulletinTopic = bulletinTopic;
	}

	public List<BulletinPublishedItem> getItens() {
		return itens;
	}

	public void setItens(List<BulletinPublishedItem> itens) {
		this.itens = itens;
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		initSuccefulMessage();
		loadBinder();
	}

	private void initSuccefulMessage() {
		boolean flag = Boolean.parseBoolean(execution.getParameter("retificado"));
		getComponentById("message").setVisible(flag);
	}

	@SuppressWarnings("unchecked")
	public List<BulletinType> getBulletinTypes(){
		BulletinTypeController controller = SpringFactory.getController("bulletinTypeController", BulletinTypeController.class, null);
		return (List<BulletinType>) controller.listAll().getList();
	}
	
	public List<BulletinTopics> getBulletinTopics(){
		if(bulletinPart == null){
			return new ArrayList<BulletinTopics>();
		} else {
			BulletinTopicsController controller = SpringFactory.getController("bulletinTopicsController", BulletinTopicsController.class, null);
			return (List<BulletinTopics>) controller.getTopicsByPart(bulletinPart);
		}
	}
	
	public List<Integer> getBulletinParts(){
		List<Integer> parts = new ArrayList<Integer>();
		for(int i = 1; i <= BULLETINPARTS; i++){
			parts.add(i);
		}
		return parts;
	}
	
	@SuppressWarnings("unchecked")
	public Return searchPublicatedItens() {
		HashMap<String, Object> data = new HashMap<>();
		data.put("entity", new BulletinPublishedItem());
		data.put("bulletin", getEntity());
		data.put("bulletinPart", getBulletinPart());
		data.put("bulletinTopic", getBulletinTopic());
		
		BulletinPublishedItensController controller = SpringFactory.getController("bulletinPublishedItensController", BulletinPublishedItensController.class, data);
		Return ret = controller.doAction("searchPublicatedItens", false);
		
		setItens((List<BulletinPublishedItem>) ret.getList());
		showResultList();
		
		loadBinder();
		return ret;
	}
	
	private void showResultList() {
		getComponentById("message").setVisible(false);
		if(getItens() == null || getItens().isEmpty()){
			getComponentById("noResult").setVisible(true);
			getComponentById("resultList").setVisible(false);
		} else {
			getComponentById("noResult").setVisible(false);
			getComponentById("resultList").setVisible(true);
		}
	}

	public void sendToRetification(BulletinPublishedItem item){
		session.setAttribute("itemToRetificate", item.getItem());
		Executions.sendRedirect(item.getItem().getBulletinTopic().getFormURI());
	}

	@Override
	public BulletinController getControl() {
		return SpringFactory.getController("bulletinController",
				BulletinController.class,
				ReflectionUtils.prepareDataForPersistence(this));
	}

	public BulletinAwaitItensController getBulletinAwaitItensController() {
		return SpringFactory.getController("bulletinAwaitItensController",
				BulletinAwaitItensController.class, null);
	}
	
	@Override
	public Bulletin getEntityObject() {
		return new Bulletin();
	}

	@Override
	protected String getUpdatePage() {
		return null;
	}

	@Override
	protected String getDeactivationMessage() {
		return null;
	}
	
}
